using System;
using System.IO;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using ACM.cmsarea08;
using ACM.cmsarea08.Core;
using ACM.cmsarea08.Controls;
using ACM.cmsarea08.Administration;

using OpenFlashChart;

namespace ACM.cmsarea08.Administration.Core {
	public partial class ViewStatChart :  System.Web.UI.Page {
		private ACM.cmsarea08.Administration.Core.DBClass dclass = new cmsarea08.Administration.Core.DBClass();
		private ACM.cmsarea08.Function.TextClass tlc = new ACM.cmsarea08.Function.TextClass();
		
		
		override protected void OnInit(EventArgs e)
		{
			InitializeComponent();
			base.OnInit(e);
			
		}

		private void InitializeComponent()
		{
			this.Load += new System.EventHandler(this.Page_Load);
		}
	
		private void Page_Load(object o , EventArgs e) {
			Response.CacheControl= "no-cache";
			DateTime dtMin =DateTime.Now;
			DateTime dtMax =DateTime.Now;
			string dtStart = "";
			string dtEnd = "";
			//Response.Write("cerco le date");
			if(Request.Params["dts"]==null)
				dtMin = DateTime.Now.AddDays(-25);
			else {
				dtStart = Request.Params["dts"].ToString();
				DateTime.TryParse(dtStart,out dtMin);
			}
			if(Request.Params["dte"]==null)
				dtMax = DateTime.Now;
			else {
				dtEnd = Request.Params["dte"].ToString();
				DateTime.TryParse(dtEnd,out dtMax);
			}			
			//this.Controls.Add(new LiteralControl("<h1>Qui</h1>"));
			
			int clickMax = 10;
			
			string resi ="";
			//prindi click massimo per data..
			//prevedere di inserire ..il parametro da ..> a 
			resi = this.OttineniValore(string.Format("select sum(clicks) as som  from {0}status where firstclick>='{1}' and firstclick<'{2}' " +
			                                         "group by year(firstclick), month(firstclick), day(firstclick) order by som desc limit 1;",dclass.TablePrefix, dtMin.ToString("yyyy-MM-dd"), dtMax.ToString("yyyy-MM-dd") ));
			if(resi!="")
				Int32.TryParse(resi,out clickMax);
			 
			//prepara la differenza ..tra dtMin e dtMax in giorni.. e considera solo gli ultimi 30 gg.
			System.TimeSpan st = new TimeSpan(dtMax.Ticks - dtMin.Ticks);
			//Response.Write("RESI : " + clickMax);
			
			ArrayList ar = new ArrayList();
			for(int i = 0 ;i<st.Days; i++)
				ar.Add(dtMin.AddDays(i).ToString("dd/MM") ) ;
			Graph graph = new Graph();
			try {	
			
	        //graph.LegendX = new LegendX("Visite" + ar.Count, 12, "#FF0000");
	        graph.StepsY = (int)(clickMax/10);
	        //graph.AxisStepsX = st.Days;
	        
	        graph.LabelsX = ar;
	        //graph.LabelStyleX = new LabelStyleX(10, "#000000", 2, 2);
	        graph.MaxY = clickMax + 10;
	        graph.BgColor ="#FFFFFF";
	        OpenFlashChart.Charts.ChartData temp;
	        temp = new OpenFlashChart.Charts.AreaHollow(2, 3, "#0000CC", 75, "Visite", 12, "#AAAAFF");
	        //ottengo il dset...con le cose...
	        //Response.Write(dtMin.ToString() + " - " + dtMax.ToString());
	        DataSet ds = dclass.Helper_Dataset(string.Format("select sum(clicks) as som  from {0}status where firstclick>='{1}' and firstclick<'{2}' " +
	                                                         "group by year(firstclick), month(firstclick), day(firstclick) order by firstclick asc ;",dclass.TablePrefix, dtMin.ToString("yyyy-MM-dd"), dtMax.ToString("yyyy-MM-dd") ));

	        if(ds!=null) {
	        	temp = new OpenFlashChart.Charts.AreaHollow(2, 3, "#0000CC", 75, ("Visite"), 12, "#AAAAFF");
	        	 int startPoint = 0;
	        	 if(ar.Count>ds.Tables[0].Rows.Count) {
	        	 	startPoint = ar.Count - ds.Tables[0].Rows.Count;
	        	 }
	        	 
	        	for(int i = 0; i<ar.Count;i++) {
	        		float clicks = 0;
	        		if(i>=startPoint) {
	        			if(i< ds.Tables[0].Rows.Count && i>-1)
	        				float.TryParse(ds.Tables[0].Rows[i][0].ToString(), out clicks);
	        		}
	        		temp.Data.Add(clicks);
	        	}
	        }
	        graph.Data.Add(temp);
			} catch (Exception ex)  {
				//Response.Write("EX. to: " + ex.ToString());
			}
	        Response.Clear();
	        Response.Write(graph.ToString());
	        Response.End();
		}
		
		
		private string OttineniValore(string query) {
			object o = dclass.HStandard_SC(query);
			if(o==null) return "";
			string reso = "";
			if(o is byte[])
				reso = System.Text.Encoding.UTF8.GetString((byte[]) o );
			else
				reso = o.ToString();
			return reso;
		}
	}
	
}
